package com.crm.system.service.impl.permissionManagement;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.sys.permissionManagement.SysAccountPermission;
import com.crm.service.sys.permissionManagement.SysAccountPermissionDbService;
import com.crm.system.service.permissionManagement.SysAccountPermissionService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ Author ：pym
 * @ Date   ：2020/12/9 14:48
 */
@Service
public class SysAccountPermissionServiceImpl implements SysAccountPermissionService {
    @Autowired
    SysAccountPermissionDbService sysAccountPermissionDbService;

    /**
     * 添加账号-权限组关联
     * @param permissionGroupId
     * @param accountIds
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void addSysAccountPermission(String permissionGroupId, String accountIds) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        if(userAccount != null){
            //删除账号-权限组关联
            sysAccountPermissionDbService.deleteSysAccountPermissionByPGId(permissionGroupId, userAccount.getAccountId());
            List<String> accountIdList = new ArrayList<String>();
            JSONArray accountArray = JSONArray.parseArray(accountIds);
            for (int i = 0; i < accountArray.size(); i++) {
                JSONObject accountObject = (JSONObject) accountArray.get(i);
                String accountIdentification = accountObject.get("accountIdentification") != null ? accountObject.get("accountIdentification").toString() : "";
                if(!"0".equals(accountIdentification)){
                    accountIdList.add(accountObject.get("id") != null ? accountObject.get("id").toString() : "");
                }
            }
            if(accountIdList != null && !accountIdList.isEmpty()){
                for (String accountId : accountIdList) {
                    SysAccountPermission sysAccountPermission = getSysAccountPermission(permissionGroupId, userAccount, accountId);
                    sysAccountPermissionDbService.insertSysAccountPermission(sysAccountPermission);
                }
            }
        }
    }

    /**
     * 创建账号-权限组关联实体
     * @param permissionGroupId
     * @param userAccount
     * @param accountId
     * @return
     */
    private SysAccountPermission getSysAccountPermission(String permissionGroupId, UserAccount userAccount, String accountId) {
        SysAccountPermission sysAccountPermission = new SysAccountPermission();
        sysAccountPermission.setAccountPermissionId(CommonUtils.getGUID());
        sysAccountPermission.setAccountId(accountId);
        sysAccountPermission.setPermissionGroupId(permissionGroupId);
        sysAccountPermission.setCreateTm(new Date());
        if(userAccount != null){
            sysAccountPermission.setCreatePeopleId(userAccount.getAccountId());
            sysAccountPermission.setCreatePeopleName(userAccount.getUserName());
        }
        sysAccountPermission.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        return sysAccountPermission;
    }

    @Override
    public List<SysAccountPermission> selectSysAccountPermissionList(String permissionGroupId, String accountId) {
        return sysAccountPermissionDbService.selectSysAccountPermissionList(permissionGroupId, accountId);
    }

    @Override
    public Boolean deleteSysAccountPermissionByPGId(String permissionGroupId, String accountId) {
        return sysAccountPermissionDbService.deleteSysAccountPermissionByPGId(permissionGroupId, accountId);
    }

    @Override
    public List<SysAccountPermission> findPermissionGroupsInfo(String accountId) {
        return sysAccountPermissionDbService.findPermissionGroupsInfo(accountId);
    }
}
